package xiaohong.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadLocalTaskWrapper {


    public static Runnable wrapRunnable(Runnable task, ThreadLocal<String> threadLocal) {
        return () -> {
            try {
                threadLocal.set("Task Context");
                task.run();
            } finally {
                threadLocal.remove();
            }
        };
    }

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        ThreadLocal<String> threadLocal = new ThreadLocal<>();

        Runnable task = wrapRunnable(() -> {
            System.out.println("Running task with ThreadLocal value: " + threadLocal.get());
        }, threadLocal);

        executor.submit(task);
        executor.shutdown();
    }


}
